﻿using System;
using RG.Common.Configuration;
using RG.Common.WPF.Commands;
using Microsoft.Practices.Unity;

namespace RG.Common.WPF.Filters.ToolBarFilters
{
    [SupportDataBinding]
    public class FilterFixedDateFrameVM : FilterBaseParamVM<DateTime?>
    {
        public FilterFixedDateFrameVM(IUnityContainer cfg):base(cfg)
        {
            Value = DateTime.Now.Date;
            ValueForBinding = DateTime.Now.Date;
        }

        private bool isUsed;

        [PropertySetting]
        public bool IsUsed
        {
            get
            {
                return isUsed;
            }
            set
            {
                isUsed = value;
                if (value)
                {
                    Value = ValueForBinding;
                }
                else
                    Value = null;
                
            }
        }

        protected override DateTime? ReturnDefaultEmptyValue()
        {
            return null;
        }

        protected override void ValueChanged()
        {
            if(Value.HasValue)
            {
                ValueForBinding = Value.Value;
            }
        }

        private DateTime valueForBinding;
        public DateTime ValueForBinding
        {
            get { return valueForBinding; }
            set
            {
                valueForBinding = value;
                if (IsUsed)
                    SetValueQuite(valueForBinding);
                OnPropertyChanged("FormattedValue");
            }
        }

        public string FormattedValue
        {
            get
            {
                return IsUsed ? String.Format("на {0:d}", Value) : EmptyText;
            }
        }

        public override void ClearValueQuiet()
        {
            Value = DateTime.Now.Date;
            IsUsed = false;
            IsSelected = false;
        }

        public override bool ValueEmpty()
        {
            return  !IsUsed;
        }
    }
}
